package com.yinhai.service;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.yinhai.dao.CloudNoteDaoImpl;
import com.yinhai.model.CloudNote;
import com.yinhai.util.GetCurrentTime;

public class CloudNoteServiceImpl {
	private Context context;
	private CloudNoteDaoImpl cloudNoteDaoImpl;
	public CloudNoteServiceImpl(Context context){
		cloudNoteDaoImpl = new CloudNoteDaoImpl(context);
		this.context = context;
	}
	
	public void insert(CloudNote cn){
		cloudNoteDaoImpl.insert(cn);
	}
	
	public void init(String phone){
		CloudNote cn = new CloudNote();
		cn.setUuid("0");
		cn.setNote("分类管理");
		cn.setPid("-1");
		cn.setCreate_time(GetCurrentTime.getTime());
		cn.setPhone(phone);
		cloudNoteDaoImpl.insert(cn);
		
		cn = new CloudNote();
		cn.setUuid("1");
		cn.setNote("默认分类");
		cn.setPid("0");
		cn.setCreate_time(GetCurrentTime.getTime());
		cn.setPhone(phone);
		cloudNoteDaoImpl.insert(cn);
		
	}
	//获取单条数据
	public CloudNote getCloudNoteByUuid(String uuid){
		CloudNote cn = null;
		String sql = "select u.* from cloud_note u where uuid='"+uuid+"'";
		List<CloudNote> cnList = cloudNoteDaoImpl.rawQuery(sql, null);
		if(cnList!=null && cnList.size()>0){
			cn = cnList.get(0);
		}else{
			return null;
		}
		return cn;
	}
	//获取分类数据
	public List<CloudNote> getPopSortList(){
		String sql = "select * from cloud_note  where pid='-1' or pid ='0'";
		return cloudNoteDaoImpl.rawQuery(sql, null);
	}
	
	//获取分组数据
	public List<CloudNote> getSortList(String pid){
		String sql = "select u.* from cloud_note u where pid='"+pid+"'";
		return cloudNoteDaoImpl.rawQuery(sql, null);
	}
	//获取所有数据
	public List<CloudNote> getAllList(){
		String sql = "select u.* from cloud_note u where pid <> '-1' and uuid <> '1'";
		return cloudNoteDaoImpl.rawQuery(sql, null);
	}
	//清除所有非默认数据
	public void delAllList(){
		String sql = "delete from cloud_note where pid <> '-1' and uuid <> '1'";
		cloudNoteDaoImpl.execSql(sql, null);
	}
	//更新数据
	public void update(String uuid,String note,String create_time){
		String sql = "update cloud_note set note='"+note+"',create_time='"+create_time+"' where uuid='"+uuid+"'";
		cloudNoteDaoImpl.execSql(sql, null);
	}
	//删除二级目录数据
	public void deleteSubData(String pid){
		String sql = "delete from cloud_note where pid='"+pid+"'";
		cloudNoteDaoImpl.execSql(sql, null);
	}
	//删除单条数据
	public void deleteMainData(String uuid){
		String sql = "delete from cloud_note where uuid='"+uuid+"'";
		cloudNoteDaoImpl.execSql(sql, null);
	}
	//删除数据
	public void delete(CloudNote cn){
		if(cn.getPid().equals("0")){
			deleteSubData(cn.getUuid());
			deleteMainData(cn.getUuid());
		}else{
			deleteMainData(cn.getUuid());
		}
	}
	//云备份字符串
	public String getCloudBuckUpData(String phone){
		String postStr = "";
		List<CloudNote> cnList = getAllList();
		try {
			JSONArray jsonArray = new JSONArray();
			if(cnList!=null && cnList.size()>0){
				for(int i=0;i<cnList.size();i++){
					JSONObject obj = new JSONObject();
					obj.put("uuid", cnList.get(i).getUuid());
					obj.put("note", cnList.get(i).getNote());
					obj.put("pid", cnList.get(i).getPid());
					obj.put("phone", cnList.get(i).getPhone());
					obj.put("create_time", cnList.get(i).getCreate_time());
					jsonArray.put(obj);
				}
			}
			JSONObject obj = new JSONObject();
			if(jsonArray!=null && jsonArray.length()>0){
				obj.put("phone", phone);
				obj.put("data",jsonArray);
				postStr = obj.toString();
			}else{
				return postStr;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return postStr;
	}
	//判断是否需要备份
	public boolean isNeedBackUp(){
		boolean flag = false;
		List<CloudNote> cnList = getAllList();
		if(cnList != null && cnList.size()>0){
			flag = true;
		}
		return flag;
	}
	//判断分类是否重复
	public boolean isSameSortName(String sort_name){
		boolean flag = false;
		String sql = "select * from cloud_note where note='"+sort_name+"'";
		List<CloudNote> cnList = cloudNoteDaoImpl.rawQuery(sql, null);
		if(cnList!=null && cnList.size()>0){
			flag = true;
		}
		return flag;
	}
	
	//清除云笔记内容
	public void deleteAllCloudNote(){
		String sql = "delete from cloud_note";
		cloudNoteDaoImpl.execSql(sql, null);
	}
}
